package com.itheima.线程安全.卖票;

import java.util.Random;

public class Demo2 {
    public static void main(String[] args) {
        MyThread2 mt1 = new MyThread2();
        MyThread2 mt2 = new MyThread2();

        mt1.setName("窗口一");
        mt2.setName("窗口二");

        mt1.start();
        mt2.start();
    }
}

class MyThread2 extends Thread {
    private static int tickCount = 40;

    @Override
    public void run() {
        while (true) {
            synchronized ("abc") {
                if (tickCount <= 0) {  //小于等于0说明没票了
                    break;
                } else {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    tickCount--;
                    System.out.println(Thread.currentThread().getName() + "在卖票，还剩下" + tickCount + "张票");
                }
            }
        }
    }
}